﻿using System;
using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneBusinessItemTypeList : UserControl, IListAction
    {

        private VodafoneController _vodafoneController;

        #region Properties

        public VodafoneController VodafoneController
        {
            set
            {
                _vodafoneController = value;
                _vodafoneController.BusinessItemTypeSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public VodafoneBusinessItemTypeList()
        {
            InitializeComponent();
            gridView.DoubleClick += delegate { Edit(); };
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            gridControl.DataSource = _vodafoneController.BusinessItemTypes;
        }

        private static TipoItemNegocio GetClone(TipoItemNegocio businessItemType)
        {
            var document = new TipoItemNegocio
                               {
                                   IdTipoItemNegocio = businessItemType.IdTipoItemNegocio,
                                   Nome = businessItemType.Nome
                               };
            return document;
        }

        private static void Merge(TipoItemNegocio businessItemType, TipoItemNegocio document)
        {
            businessItemType.Nome = document.Nome;
        }

        private void ShowBusinessItemType(TipoItemNegocio businessItemType)
        {
            TipoItemNegocio document = GetClone(businessItemType);
            var window = new VodafoneBusinessItemType {Document = document};
            if (DialogResult.OK != window.ShowDialog())
                return;
            Merge(businessItemType, window.Document);
            _vodafoneController.SaveBusinessItemType(businessItemType);
        }

        #endregion

        #region IListAction

        public bool IsEditVisible
        {
            get { return true; }
        }

        public bool IsEditEnable
        {
            get { return true; }
        }

        public void Edit()
        {
            var row = (TipoItemNegocio)gridView.GetRow(gridView.FocusedRowHandle);
            if (row == null)
                return;
            ShowBusinessItemType(row);
        }

        public void Add()
        {
            var businessItemType = new TipoItemNegocio {IdTipoItemNegocio = -1, Nome = string.Empty};
            ShowBusinessItemType(businessItemType);
        }

        public bool IsAddVisible
        {
            get { return true; }
        }

        public bool IsAddEnable
        {
            get { return true; }
        }

        public void ChangeSituation()
        {
            throw new NotImplementedException();
        }

        public bool IsChangeSituationVisible
        {
            get { return false; }
        }

        public bool IsChangeSituationEnable
        {
            get { return false; }
        }

        public void Remove()
        {
            throw new NotImplementedException();
        }

        public bool IsRemoveVisible
        {
            get { return false; }
        }

        public bool IsRemoveEnable
        {
            get { return false; }
        }

        #endregion
    }
}
